﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Actuals.CoreDomain.Business;
using Actuals.Business;
using Actuals.CoreDomain;

namespace Actuals
{
    public partial class Tasks : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["EmployeeId"] == null)
            {
                Response.Write("Please login!");
                Response.End();
            }
            if (!IsPostBack)
            {
                IBusinessProject businessproject = new BusinessProject();
                List<IProject> projects = businessproject.GetProjects();
                ddlProject.DataSource = projects;
                ddlProject.DataTextField = "ProjectDescription";
                ddlProject.DataValueField = "ProjectId";
                ddlProject.DataBind();
                ddlProject.Items.Insert(0, "Select");
            }
        }


              
                

       

        protected void FillRelease(string projectId)
        {
            IBusinessRelease businessRelease = new BusinessRelease ();
            IList<IRelease> releases = businessRelease.GetReleases(int.Parse(projectId));
            ddlRelease.DataSource = releases;
            ddlRelease.DataTextField = "Description";
            ddlRelease.DataValueField = "ReleaseId";
            ddlRelease.DataBind();
            ListItem select =  new ListItem();
            select.Value = "0";
            select.Text = "Select";
            ddlRelease.Items.Insert(0,select);
        }


        protected void ddlProject_OnSelectedIndexChanged(Object sender, EventArgs e)
        {
            FillRelease(ddlProject.SelectedValue);
        }

        protected void gvResults_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            string taskId;

              switch (e.CommandName)
            {
                case "EditActual":
                    taskId = e.CommandArgument.ToString();
                   // int TaskId =int.Parse( taskId);
                     /* ClientScriptManager cs = this.ClientScript;
                      cs.RegisterStartupScript(this.GetType(), "ShowUpdateAddTasks", "ShowUpdateAddTasks('" + taskId + "');", true);*/
                      ScriptManager.RegisterClientScriptBlock(this,this.GetType(), "ShowUpdateAddTasks", "ShowUpdateAddTasks('" + taskId + "');", true);
                   /* ((System.Web.UI.Page)System.Web.HttpContext.Current.Handler).ClientScript.RegisterStartupScript
                   (typeof(Tasks), "ShowUpdateAddTasks", "ShowUpdateAddTasks('TaskId');", true);
                    //Response.Redirect("AddTasks.aspx?Mode=Edit&TaskId=" + taskId);*/
                    
                    break;
            }
        }


        protected void BindGridView()
             {
                 int projectId;
                 projectId = (int.Parse(ddlProject.SelectedValue));
                 int releaseId = (int.Parse(ddlRelease.SelectedValue));
                IBusinessTask businessTask = new BusinessTask();
                 IList<ITask> tasks = businessTask.GetTasks(projectId,releaseId);

                ViewState["DataSource"] = tasks;
                gvResults.DataSource = tasks;
                gvResults.DataBind();
            }
        IList<IActual> actuals = new List<IActual>();

        protected void btnGetTasks_Click(Object sender, EventArgs e)
        {
            BindGridView();
        }


        protected void btnDeleteTask_Click(object sender, EventArgs e)
        {
            try
            {

                for (int i = 0; i < gvResults.Rows.Count; i++)
                {

                    CheckBox cb = (CheckBox)gvResults.Rows[i].FindControl("chkTask");
                    Label lbl = (Label)gvResults.Rows[i].FindControl("lblTaskId");
                    if (cb.Checked)
                    {
                        string taskId = string.Empty;
                        if (lbl != null)
                        {
                            taskId = lbl.Text;
                            BusinessTask businessTask = new BusinessTask();
                            businessTask.DeleteTask(int.Parse(taskId));
                        }
                    }
                }
                lblMsg.Text = "Deleted";
                BindGridView();
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }
        }


        


        
    }
}


